#include "slider.hpp"

#include "core/assert.hpp"
#include "ui/controls/control_base.hpp"
#include <imgui.h>

void slider::update_data (bindable_base *data)
{
    value_->attach(data);
    min_->attach(data);
    max_->attach(data);
}

void slider::init_content(const xml_node &node)
{
    ui_control::init_content(node);
    value_ = create_data(node.attribute("value").as_string(), number);
    min_ = create_data(node.attribute("min").as_string(), number);
    max_ = create_data(node.attribute("max").as_string(), number);

    PI_ASSERT_MSG(value_ && min_ && max_, "slider require value,min,max attribute!!");
}

void slider::draw_self()
{
    switch (value_->type ())
    {
    case signed_integral: {
        s64       val = value_->get_s64();
        const s64 max = max_->get_s64();
        const s64 min = min_->get_s64();
        if (ImGui::SliderScalar ("##Value", ImGuiDataType_S64, &val, &min, &max))
        {
            value_->set_s64(val);
        }
        break;
    }
    case unsigned_integral: {
        u64       val = value_->get_u64();
        const u64 max = max_->get_u64();
        const u64 min = min_->get_u64();
        if (ImGui::SliderScalar("##Value", ImGuiDataType_U64, &val, &min, &max))
        {
            value_->set_u64(val);
        }
        break;
    }
    case floating_point: {
        f64       val = value_->get_f64();
        const f64 max = max_->get_f64();
        const f64 min = min_->get_f64();
        if (ImGui::SliderScalar("##Value", ImGuiDataType_Double, &val, &min, &max))
        {
            value_->set_f64(val);
        }
        break;
    }
    default: PI_ASSERT_MSG(0, "???");
    }
}
